package com.jml.account.sysMenu;

import com.jml.account.sysMenu.dao.SysMeanMapper;
import com.jml.account.sysMenu.entity.SysMean;
import com.jml.account.sysMenu.entity.SysMeanExample;
import com.jml.account.sysMenu.entity.SysMeanExample.Criteria;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class Main {

	public static void main(String[] args) throws IOException {
		SqlSession session = null;
		try {
			session = getMybatisSession("conf.xml");
			getSqlResult(session);
		} finally {
			sessionClose(session);
		}
	}

	private static void getSqlResult(SqlSession session) {
		SysMeanMapper sysMeanMapper = session.getMapper(SysMeanMapper.class);
		SysMeanExample sysMeanExample = new SysMeanExample();
		Criteria criteria = sysMeanExample.createCriteria();
		//criteria.andAgeGreaterThan(2);
		sysMeanExample.setOrderByClause("age desc");

		//sysMeanMapper.getaa();
		SysMean sysMean = new SysMean(5,"55",55);

		sysMeanMapper.insert(sysMean);
		List<SysMean> sysMeans = sysMeanMapper.selectByExample(sysMeanExample);
		for (int i = 0; i < sysMeans.size(); i++) {
			System.out.println(sysMeans.get(i).getName());
		}
		System.out.println();
		session.commit();
	}

	private static void sessionClose(SqlSession session ) {
		if(session!=null){
			session.close();
		}
	}

	private static SqlSession getMybatisSession(String resource) throws IOException {
		// 读取配置文件
		Reader reader = Resources.getResourceAsReader(resource);
		// 获取会话工厂
		// 这个test参数就是对于mybatis的配置文件中environment的配置，选择指定的数据源
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader,"test");
		return sqlSessionFactory.openSession();
	}
}
